import { Controller, Get, Post, Req, Res } from '@nestjs/common'
import { YunService } from './yun.service'
import { Request, Response } from 'express'
import { OrderService } from '../../order/order.service'

interface HeaderData {
  // 实收金额
  actualAmount: number
  // 实付金额
  actualPayAmount: number
  // 销售订单数量
  salesOrderCount: number
  // 人均消费金额
  perCapitaConsumption: number
  // 新客
  newCustomer: number
  // 老客
  oldCustomer: number
}

@Controller('yun')
export class YunController {
  constructor(
    private readonly YunService: YunService,
    private readonly OrderService: OrderService,
  ) {}

  @Post('headerData')
  getHeaderData(@Req() req: Request, @Res() res: Response) {
    const data: HeaderData = {} as HeaderData
    res.send(data)
  }

  @Get('SearchOptions')
  async getSearchOptions(@Req() req: Request, @Res() res: Response) {
    const {
      brand = '',
      partition = '',
      shop = '',
      source = '',
      status = '',
      timeRange = '7',
    } = req.query
    let data = await this.OrderService.getorderlist()
    const date = this.YunService.generateDays(String(timeRange))
    const result = this.YunService.OrderStatistics(String(timeRange), data)
    const AddCustomer = {
      date: date,
      data: date.map((e) => this.YunService.getTotalPriceByDate(e, data)),
    }
    const filterData = this.YunService.filterOrders
    result['validOrderPercent'] = Number(
      (result.PerCapitaConsumptionAmount / result.SalesOrderNumber) * 100,
    ).toFixed(2)
    const SelectOptions = this.YunService.getSearchOptions()
    res.send({
      result,
      // price,
      AddCustomer,
      SelectOptions,
      filterData,
    })
  }
}
